package com.kele.system.util;

import com.alibaba.fastjson.JSON;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.core.context.SecurityContextHolder;

import java.io.Serializable;
import java.time.LocalDateTime;

/**
 * @Description:
 * @Author: Kele
 * @CreateDate: 2023/4/17 20:04
 * @Version:
 */
public class SecurityUtil {

    private static final Logger log = LoggerFactory.getLogger(SecurityUtil.class);


    public static User getUser() {
        try {
            Object principalObj = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            if (principalObj instanceof String) {
                //取出用户身份信息
                String principal = principalObj.toString();
                //将json转成对象
                return JSON.parseObject(principal, User.class);
            }
        } catch (Exception e) {
            log.error("获取当前登录用户身份出错:{}", e.getMessage());
            e.printStackTrace();
        }
        return null;
    }


    public static class User implements Serializable {

        private static final long serialVersionUID = 1L;

        private String id;

        // 用户名
        private String username;

        // 密码
        private String password;

        // 盐
        private String salt;

        // 昵称
        private String name;

        // 昵称
        private String nickname;

        // 微信UnionId
        private String wxUnionId;

        // 机构ID
        private String companyId;

        // 头像
        private String userPic;

        // 用户类型
        private String userType;

        // 生日
        private LocalDateTime birthday;

        // 性别
        private String sex;

        // 邮箱
        private String email;

        // 手机
        private String cellphone;

        // qq
        private String qq;

        // 用户状态
        private String status;

        // 创建时间
        private LocalDateTime createTime;

        // 更新时间
        private LocalDateTime updateTime;

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

        public String getUsername() {
            return username;
        }

        public void setUsername(String username) {
            this.username = username;
        }

        public String getPassword() {
            return password;
        }

        public void setPassword(String password) {
            this.password = password;
        }

        public String getSalt() {
            return salt;
        }

        public void setSalt(String salt) {
            this.salt = salt;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getNickname() {
            return nickname;
        }

        public void setNickname(String nickname) {
            this.nickname = nickname;
        }

        public String getWxUnionId() {
            return wxUnionId;
        }

        public void setWxUnionId(String wxUnionId) {
            this.wxUnionId = wxUnionId;
        }

        public String getCompanyId() {
            return companyId;
        }

        public void setCompanyId(String companyId) {
            this.companyId = companyId;
        }

        public String getUserPic() {
            return userPic;
        }

        public void setUserPic(String userPic) {
            this.userPic = userPic;
        }

        public String getUserType() {
            return userType;
        }

        public void setUserType(String userType) {
            this.userType = userType;
        }

        public LocalDateTime getBirthday() {
            return birthday;
        }

        public void setBirthday(LocalDateTime birthday) {
            this.birthday = birthday;
        }

        public String getSex() {
            return sex;
        }

        public void setSex(String sex) {
            this.sex = sex;
        }

        public String getEmail() {
            return email;
        }

        public void setEmail(String email) {
            this.email = email;
        }

        public String getCellphone() {
            return cellphone;
        }

        public void setCellphone(String cellphone) {
            this.cellphone = cellphone;
        }

        public String getQq() {
            return qq;
        }

        public void setQq(String qq) {
            this.qq = qq;
        }

        public String getStatus() {
            return status;
        }

        public void setStatus(String status) {
            this.status = status;
        }

        public LocalDateTime getCreateTime() {
            return createTime;
        }

        public void setCreateTime(LocalDateTime createTime) {
            this.createTime = createTime;
        }

        public LocalDateTime getUpdateTime() {
            return updateTime;
        }

        public void setUpdateTime(LocalDateTime updateTime) {
            this.updateTime = updateTime;
        }

        @Override
        public String toString() {
            return "User{" +
                    "id='" + id + '\'' +
                    ", username='" + username + '\'' +
                    ", password='" + password + '\'' +
                    ", salt='" + salt + '\'' +
                    ", name='" + name + '\'' +
                    ", nickname='" + nickname + '\'' +
                    ", wxUnionId='" + wxUnionId + '\'' +
                    ", companyId='" + companyId + '\'' +
                    ", userPic='" + userPic + '\'' +
                    ", userType='" + userType + '\'' +
                    ", birthday=" + birthday +
                    ", sex='" + sex + '\'' +
                    ", email='" + email + '\'' +
                    ", cellphone='" + cellphone + '\'' +
                    ", qq='" + qq + '\'' +
                    ", status='" + status + '\'' +
                    ", createTime=" + createTime +
                    ", updateTime=" + updateTime +
                    '}';
        }
    }

}
